home *** CD-ROM | disk | FTP | other *** search
/ Amiga Plus 1995 #2 / Amiga Plus CD - 1995 - No. 2.iso / pd / grafik / mandelsquare-ham / readilbm.c < prev    next >
C/C++ Source or Header  |  1995-04-11  |  8KB  |  442 lines

  1. /** Revision Header * Header built automatically - do not edit! *************
  2.  *
  3.  *    (C) Copyright 1991 by Olaf `Olsen' Barthel, all rights reserved
  4.  *
  5.  *    Name .....: ReadILBM.c
  6.  *    Created ..: Monday 26-Aug-91 11:20
  7.  *    Revision .: 1
  8.  *
  9.  *    Date            Author          Comment
  10.  *    =========       ========        ====================
  11.  *    26-Aug-91    Olsen        Created this file!
  12.  *
  13.  ***************************************************************************/
  14.  
  15. #define BUFFER_SIZE    16384
  16.  
  17. STATIC BYTE     FileBuffer[BUFFER_SIZE],
  18.         *BufferPtr;
  19.  
  20. STATIC LONG     BytesInBuffer = 0;
  21.  
  22. extern struct IntuitionBase *IntuitionBase;
  23.  
  24. LONG __regargs
  25. FillBuffer(struct IFFHandle *Handle)
  26. {
  27.     BytesInBuffer = ReadChunkBytes(Handle,FileBuffer,BUFFER_SIZE);
  28.  
  29.     BufferPtr = FileBuffer;
  30.  
  31.     return(BytesInBuffer);
  32. }
  33.  
  34. STATIC LONG __regargs
  35. SkipBuffer(struct IFFHandle *Handle,LONG Size)
  36. {
  37.     LONG BytesSkipped = 0,Take;
  38.  
  39.     while(Size)
  40.     {
  41.         if(!BytesInBuffer)
  42.         {
  43.             if(!FillBuffer(Handle))
  44.                 return(BytesSkipped);
  45.         }
  46.  
  47.         Take = Size > BytesInBuffer ? BytesInBuffer : Size;
  48.  
  49.         BytesSkipped    += Take;
  50.         Size        -= Take;
  51.         BufferPtr    += Take;
  52.         BytesInBuffer    -= Take;
  53.     }
  54.  
  55.     return(BytesSkipped);
  56. }
  57.  
  58. STATIC LONG __regargs
  59. ReadBuffer(struct IFFHandle *Handle,BYTE *Mem,LONG Size)
  60. {
  61.     LONG BytesRead = 0,Take;
  62.  
  63.     while(Size)
  64.     {
  65.         if(!BytesInBuffer)
  66.         {
  67.             if(!FillBuffer(Handle))
  68.                 return(BytesRead);
  69.         }
  70.  
  71.         Take = Size > BytesInBuffer ? BytesInBuffer : Size;
  72.  
  73.         BytesRead    += Take;
  74.         Size        -= Take;
  75.         BytesInBuffer    -= Take;
  76.  
  77.         while(Take--)
  78.             *Mem++ = *BufferPtr++;
  79.     }
  80.  
  81.     return(BytesRead);
  82. }
  83.  
  84. STATIC BYTE
  85. GetBuffer(struct IFFHandle *Handle)
  86. {
  87.     if(!BytesInBuffer)
  88.     {
  89.         if(!FillBuffer(Handle))
  90.             return(-128);
  91.     }
  92.  
  93.     BytesInBuffer--;
  94.  
  95.     return(*BufferPtr++);
  96. }
  97.  
  98.     /* OpenImageFile(UBYTE *Name):
  99.      *
  100.      *    Open an IFF-ILBM file with given name.
  101.      */
  102.  
  103. struct IFFHandle * __regargs
  104. OpenImageFile(UBYTE *Name)
  105. {
  106.     struct IFFHandle *Handle;
  107.  
  108.     if(Handle = AllocIFF())
  109.     {
  110.         if(Handle -> iff_Stream = Open(Name,MODE_OLDFILE))
  111.         {
  112.             InitIFFasDOS(Handle);
  113.  
  114.             if(!OpenIFF(Handle,IFFF_READ))
  115.                 return(Handle);
  116.  
  117.             Close(Handle -> iff_Stream);
  118.         }
  119.  
  120.         FreeIFF(Handle);
  121.     }
  122.  
  123.     return(NULL);
  124. }
  125.  
  126.     /* CloseImageFile(struct IFFHandle *Handle):
  127.      *
  128.      *    Close an IFF-ILBM file opened by OpenImageFile().
  129.      */
  130.  
  131. VOID __regargs
  132. CloseImageFile(struct IFFHandle *Handle)
  133. {
  134.     CloseIFF(Handle);
  135.  
  136.     Close(Handle -> iff_Stream);
  137.  
  138.     FreeIFF(Handle);
  139. }
  140.  
  141.     /* ReadImageBody():
  142.      *
  143.      *    Reads the image data contained in a standard 4 bit IFF-ILBM
  144.      *    image file.
  145.      */
  146.  
  147. BYTE __regargs
  148. ReadImageBody(struct IFFHandle *Handle,struct BitMap *BitMap,struct BitMapHeader *BitMapHeader)
  149. {
  150.     USHORT             i,j,k;
  151.     UBYTE             Value,Compr,Depth;
  152.     BYTE             ChkVal;
  153.     LONG             Height,Width,SoFar;
  154.     PLANEPTR         Planes[9];
  155.  
  156.     struct ContextNode    *ContextNode;
  157.  
  158.     BytesInBuffer = 0;
  159.  
  160.     if(ContextNode = CurrentChunk(Handle))
  161.     {
  162.         Width    = BitMap -> BytesPerRow;
  163.         Height    = BitMap -> Rows;
  164.         Depth    = BitMapHeader -> nPlanes;
  165.         Compr    = BitMapHeader -> compression;
  166.  
  167.         if(Compr > 1 || Depth < 1)
  168.             return(FALSE);
  169.  
  170.         for(i = 0 ; i < Depth ; i++)
  171.             Planes[i] = BitMap -> Planes[i];
  172.  
  173.         for(i = Depth ; i < 9 ; i++)
  174.             Planes[i] = NULL;
  175.  
  176.         if(BitMapHeader -> masking == 1)
  177.             Depth++;
  178.  
  179.         if(Compr == 0)
  180.         {
  181.             for(k = 0 ; k < Height ; k++)
  182.             {
  183.                 for(j = 0 ; j < Depth ; j++)
  184.                 {
  185.                     if(Planes[j])
  186.                     {
  187.                         if(ReadBuffer(Handle,Planes[j],Width) != Width)
  188.                             return(FALSE);
  189.                         else
  190.                             Planes[j] += Width;
  191.                     }
  192.                     else
  193.                     {
  194.                         if(SkipBuffer(Handle,Width) != Width)
  195.                             return(FALSE);
  196.                     }
  197.                 }
  198.             }
  199.         }
  200.  
  201.         if(Compr == 1)
  202.         {
  203.             for(k = 0 ; k < Height ; k++)
  204.             {
  205.                 for(j = 0 ; j < Depth ; j++)
  206.                 {
  207.                     SoFar = Width;
  208.  
  209.                     if(Planes[j])
  210.                     {
  211.                         while(SoFar > 0)
  212.                         {
  213.                             if((ChkVal = GetBuffer(Handle)) != -128)
  214.                             {
  215.                                 if(ChkVal > 0)
  216.                                 {
  217.                                     LONG Count = ChkVal + 1;
  218.  
  219.                                     SoFar -= Count;
  220.  
  221.                                     if(SoFar < 0)
  222.                                         return(FALSE);
  223.                                     else
  224.                                     {
  225.                                         if(ReadBuffer(Handle,Planes[j],Count) != Count)
  226.                                             return(FALSE);
  227.  
  228.                                         Planes[j] += Count;
  229.                                     }
  230.                                 }
  231.                                 else
  232.                                 {
  233.                                     LONG Count = (-ChkVal) + 1;
  234.  
  235.                                     SoFar -= Count;
  236.  
  237.                                     if(SoFar < 0)
  238.                                         return(FALSE);
  239.                                     else
  240.                                     {
  241.                                         Value = (UBYTE)GetBuffer(Handle);
  242.  
  243.                                         while(Count--)
  244.                                             *Planes[j]++ = Value;
  245.                                     }
  246.                                 }
  247.                             }
  248.                         }
  249.                     }
  250.                     else
  251.                     {
  252.                         while(SoFar > 0)
  253.                         {
  254.                             if((ChkVal = GetBuffer(Handle)) != -128)
  255.                             {
  256.                                 if(ChkVal > 0)
  257.                                 {
  258.                                     LONG Count = ChkVal + 1;
  259.  
  260.                                     SoFar -= Count;
  261.  
  262.                                     if(SoFar < 0)
  263.                                         return(FALSE);
  264.                                     else
  265.                                     {
  266.                                         if(SkipBuffer(Handle,Count) != Count)
  267.                                             return(FALSE);
  268.                                     }
  269.                                 }
  270.                                 else
  271.                                 {
  272.                                     SoFar -= (-ChkVal) + 1;
  273.  
  274.                                     if(SoFar < 0)
  275.                                         return(FALSE);
  276.                                     else
  277.                                         GetBuffer(Handle);
  278.                                 }
  279.                             }
  280.                         }
  281.                     }
  282.                 }
  283.             }
  284.         }
  285.  
  286.         return(TRUE);
  287.     }
  288.     else
  289.         return(FALSE);
  290. }
  291.  
  292.     /* ReadImageHeader():
  293.      *
  294.      *    Reads the header information, viewmodes and colours contained
  295.      *    in an IFF-ILBM file.
  296.      */
  297.  
  298. BYTE __regargs
  299. ReadImageHeader(struct IFFHandle *Handle,ULONG *ViewModes,BYTE *NumCols,UWORD *Colours,struct BitMapHeader *BitMapHeader,struct MandelInfo *MandelInfo)
  300. {
  301.     STATIC ULONG ImageProps[] =
  302.     {
  303.         'ILBM','BMHD',
  304.         'ILBM','CMAP',
  305.         'ILBM','CAMG',
  306.         'ILBM','MAND'
  307.     };
  308.  
  309.     struct StoredProperty *Prop;
  310.  
  311.     if(!PropChunks(Handle,&ImageProps[0],4))
  312.     {
  313.         if(!StopChunk(Handle,'ILBM','BODY'))
  314.         {
  315.             if(!ParseIFF(Handle,IFFPARSE_SCAN))
  316.             {
  317.                 if(Prop = FindProp(Handle,'ILBM','BMHD'))
  318.                 {
  319.                     CopyMem(Prop -> sp_Data,BitMapHeader,sizeof(struct BitMapHeader));
  320.  
  321.                     if(BitMapHeader -> nPlanes > 0 && BitMapHeader -> nPlanes <= 8)
  322.                     {
  323.                         if(MandelInfo)
  324.                         {
  325.                             if(Prop = FindProp(Handle,'ILBM','MAND'))
  326.                             {
  327.                                 MandelInfo -> Iterations = 32;
  328.  
  329.                                 CopyMem(Prop -> sp_Data,MandelInfo,Prop -> sp_Size);
  330.                             }
  331.                             else
  332.                                 MandelInfo -> Iterations = 0;
  333.                         }
  334.  
  335.                         if(ViewModes)
  336.                         {
  337.                             extern struct GfxBase *GfxBase;
  338.  
  339.                             *ViewModes = NULL;
  340.  
  341.                             if(BitMapHeader -> pageHeight > ((GfxBase -> DisplayFlags & PAL) ? 256 : 200))
  342.                                 *ViewModes |= LACE;
  343.  
  344.                             if(BitMapHeader -> pageWidth >= 640)
  345.                                 *ViewModes |= HIRES;
  346.  
  347.                             if(BitMapHeader -> nPlanes == 6)
  348.                                 *ViewModes |= HAM;
  349.  
  350.                             if(Prop = FindProp(Handle,'ILBM','CAMG'))
  351.                                 *ViewModes = *(ULONG *)Prop -> sp_Data;
  352.  
  353.                             *ViewModes &= ~(SPRITES | VP_HIDE | GENLOCK_AUDIO | GENLOCK_VIDEO);
  354.  
  355.                             if(ModeNotAvailable(*ViewModes))
  356.                                 *ViewModes &= 0xFFFF;
  357.  
  358.                             if(ModeNotAvailable(*ViewModes))
  359.                                 return(FALSE);
  360.                         }
  361.  
  362.                         if(NumCols && Colours)
  363.                         {
  364.                             UBYTE    *ColourArray;
  365.                             WORD     i,R,G,B;
  366.  
  367.                             if(!(Prop = FindProp(Handle,'ILBM','CMAP')))
  368.                                 return(FALSE);
  369.  
  370.                             ColourArray = (UBYTE *)Prop -> sp_Data;
  371.  
  372.                             *NumCols = Prop -> sp_Size / 3;
  373.  
  374.                             for(i = 0 ; i < *NumCols ; i++)
  375.                             {
  376.                                 R = ColourArray[i * 3 + 0] >> 4;
  377.                                 G = ColourArray[i * 3 + 1] >> 4;
  378.                                 B = ColourArray[i * 3 + 2] >> 4;
  379.  
  380.                                 Colours[i] = (R << 8) | (G << 4) | (B);
  381.                             }
  382.                         }
  383.  
  384.                         return(TRUE);
  385.                     }
  386.                 }
  387.             }
  388.         }
  389.     }
  390.  
  391.     return(FALSE);
  392. }
  393.  
  394.     /* CreateBitMap(BYTE Depth,UWORD Width,UWORD Height):
  395.      *
  396.      *    Creates and initializes a BitMap structure with
  397.      *    given dimensions.
  398.      */
  399.  
  400. struct BitMap * __regargs
  401. CreateBitMap(BYTE Depth,UWORD Width,UWORD Height)
  402. {
  403.     struct BitMap    *BitMap;
  404.     BYTE         i;
  405.  
  406.     if(BitMap = (struct BitMap *)AllocVec(sizeof(struct BitMap),MEMF_PUBLIC|MEMF_CLEAR))
  407.     {
  408.         LONG PlaneSize;
  409.  
  410.         InitBitMap(BitMap,Depth,Width,Height);
  411.  
  412.         PlaneSize = BitMap -> BytesPerRow * BitMap -> Rows;
  413.  
  414.         if(!(BitMap -> Planes[0] = AllocVec(PlaneSize * BitMap -> Depth,MEMF_CHIP|MEMF_PUBLIC|MEMF_CLEAR)))
  415.         {
  416.             FreeVec(BitMap);
  417.  
  418.             return(NULL);
  419.         }
  420.         else
  421.         {
  422.             for(i = 1 ; i < Depth ; i++)
  423.                 BitMap -> Planes[i] = &BitMap -> Planes[i - 1][PlaneSize];
  424.         }
  425.     }
  426.  
  427.     return(BitMap);
  428. }
  429.  
  430.     /* DeleteBitMap(struct BitMap *BitMap):
  431.      *
  432.      *    Deallocates any BitMap allocated by CreateBitMap().
  433.      */
  434.  
  435. VOID __regargs
  436. DeleteBitMap(struct BitMap *BitMap)
  437. {
  438.     FreeVec(BitMap -> Planes[0]);
  439.  
  440.     FreeVec(BitMap);
  441. }
  442.